package com.tianlu.store.controller;
 
import com.tianlu.store.common.*;
import com.tianlu.store.entity.HomeSwiper;
import com.tianlu.store.service.HomeSwiperService;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import java.sql.Wrapper;
import java.util.Objects;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 梁东烂
 * @Desc 首页轮播图(HomeSwiper)表控制层
 * @Date 2023-03-08 17:35:31
 */
@Api(tags = "首页轮播图")
@RestController
@RequestMapping("/api/homeSwiper")
@RequiredArgsConstructor
public class HomeSwiperController {
	@Resource
    private HomeSwiperService homeSwiperService;
 
	
	@ApiOperation("首页轮播图列表(分页）接口")
    @PostMapping("/findPage")
    @NoCheckToken
    public ResponseResult<PageResult<HomeSwiper>> findPage(@RequestBody HomeSwiper homeSwiper) {
        return ResponseResult.success(homeSwiperService.findPage(homeSwiper));
    }
	
	@ApiOperation("首页轮播图列表(不分页）接口")
    @PostMapping("/findList")
    @NoCheckToken
    public ResponseResult<List<HomeSwiper>> findList(@RequestBody HomeSwiper homeSwiper) {
        return ResponseResult.success(homeSwiperService.findList(homeSwiper));
    }
	
	@ApiOperation("查看首页轮播图接口")
    @GetMapping(value = "/detail")
    public ResponseResult<HomeSwiper> detail(@RequestParam Long id) {
        return ResponseResult.success(homeSwiperService.getById(id));
    }

    @ApiOperation("添加首页轮播图接口")
    @PostMapping("/batchSave")
    public ResponseResult<Boolean> batchSave(@RequestBody List<HomeSwiper> homeSwiperList) {
        homeSwiperService.removeAll();
        homeSwiperList.forEach(item -> item.setId(IdGen.snowflakeId()));
        return ResponseResult.success(homeSwiperService.saveBatch(homeSwiperList));
    }
	
	@ApiOperation("添加首页轮播图接口")
    @PostMapping("/save")
    public ResponseResult<Boolean> save(@RequestBody HomeSwiper homeSwiper) {
        homeSwiper.setId(IdGen.snowflakeId());
        return ResponseResult.success(homeSwiperService.save(homeSwiper));
    }
	
	@ApiOperation("修改首页轮播图接口")
    @PostMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody HomeSwiper homeSwiper) {
        if (Objects.isNull(homeSwiper.getId())) {
            return ResponseResult.fail(ResultCode.CODE_400, "ID不能为空");
        }
        return ResponseResult.success(homeSwiperService.updateById(homeSwiper));
    }
	
	@ApiOperation("删除首页轮播图接口")
    @PostMapping("/deleteByIds")
    public ResponseResult<Boolean> deleteByIds(@RequestParam List<Long> ids) {
        return ResponseResult.success(homeSwiperService.removeByIds(ids));
    }
 
}
